home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The X-Philes (2nd Revision)
/
The X-Philes Number 1 (1995).iso
/
xphiles
/
hp48_1
/
ureduce
< prev
next >
Wrap
Internet Message Format
|
1995-03-31
|
4KB
From comp.sys.handhelds Mon Feb 11 13:37:35 1991
Path: mentor.cc.purdue.edu!noose.ecn.purdue.edu!samsung!uunet!decwrl!pa.dec.com!rust.zso.dec.com!shlump.nac.dec.com!jareth.enet.dec.com!edp
From: edp@jareth.enet.dec.com (Eric Postpischil (Always mount a scratch monkey.))
Newsgroups: comp.sys.handhelds
Subject: Units
Message-ID: <20016@shlump.nac.dec.com>
Date: 8 Feb 91 21:29:31 GMT
Sender: newsdaemon@shlump.nac.dec.com
Reply-To: edp@jareth.enet.dec.com (Eric Postpischil (Always mount a scratch monkey.))
Organization: Digital Equipment Corporation
Lines: 124
Somebody asked for something like an opposite to UBASE -- a program that would
convert a unit expression like 1_kg*m^2/s^3 to 1_W. Here is something people
can try out. UREDUCE takes a unit object in level 2 and a list of preferred
units in level 1. It then tries to represent the given object in terms of the
preferred units.
The method used to do this is fairly primitive. For each preferred unit, the
program tests to see whether multiplying or dividing the given unit by the
preferred unit results in a reduced, or "better", unit expression. "Better"
is defined in this case as: Of two unit expressions, the one with the lesser
sum of the squares of the exponents of the UBASE units. I.e., reducing the
magnitude of the exponents makes a better expression, and reducing large
exponents is better than reducing small exponents. In effect, the program
multiplies or divides by a preferred unit until the remaining expression does
not become better, then it moves on to the next preferred unit.
This method has the advantge that 1_kg*m^2/s^4 and 1_kg*m/s^2 are converted,
given a preference list of { 1_W }, to 1_W/s and 1_W*s, respectively -- the
program is not stymied by negative values or by trying to make exact matches.
What order should you put things in the preference list? The things you want
to see most should go first. Be aware that these are going to override things
that come later. If you have both a unit of power and a unit of energy in the
list, chances are that the results will come out in terms of the first unit,
even if the second unit is a better fit. This program is really just a testing
of the waters -- try it out and let me know what suggestions you have for
improvements. This program is also not optimized yet. I suspect it might be
faster to do the unit manipulations using arrays.
Another algorithm might be to have the user supply a set of 8 units (seven
physical units plus one user-defined unit). If the user-supplied 8 units were
linearly independent, it would be possible to write a program that converted
any given unit expression into an expression using the user-supplied units.
This conversion would be exact, without the heuristic method used above. I
think it should be faster as well. This still wouldn't convert complex unit
expressions to things like energy or viscosity, but it would allow conversion to
cgs or English base units.
The source is below. It uses Bill Wickes' DIMS, so I am including that.
-- edp (Eric Postpischil)
"Always mount a scratch monkey."
edp@jareth.enet.dec.com
Download this, convert with ASC->, and store in 'DIMS':
%%HP: T(3)A(D)F(.);
"D9D20D29512BF81B7040D9D2044EF0A211693045FC436ADB46AAC35F30403C37
088130E4A2070000FF40D35D53453392020000000000072102C230178A2CB916
D9D20339202000000000006520189A2B21303223019D35433706B436AAC35442
30B2130B21303587"
Download:
%%HP: T(3)A(R)F(.);
DIR
UREDUCE
\<< 1 \-> remain
preferred result
\<< 1 preferred
SIZE
FOR i
'result' preferred
i GET remain OVER
BEST ^ DUP 'remain'
STO* STO/
NEXT result
remain UBASE *
\>>
\>>
UGOOD
\<<
IF DUP TYPE
13 \=/
THEN DROP 0
ELSE DIMS 8
\->ARRY ABS
END
\>>
BETTER
\<< \-> goodc good
testc test
\<<
IF test
good <
THEN testc
test 1
ELSE goodc
good 0
END
\>>
\>>
BESTM
\<< \-> input unit
\<< -1
WHILE unit
OVER ^ input *
UGOOD BETTER
REPEAT OVER
1 -
END
\>>
\>>
BESTP
\<< \-> input unit
\<< 1
WHILE unit
OVER ^ input *
UGOOD BETTER
REPEAT OVER
1 +
END
\>>
\>>
BEST
\<< \-> input unit
\<< 0 input
UGOOD input unit
BESTP input unit
BESTM DROP
\>>
\>>
END